import Vue from "vue"
// 依赖路由 导入vue-router插件
import VueRouter from "vue-router"
import auth from '@/utils/auth'
// 使用vue-router插件
Vue.use(VueRouter)
// 导入组件（第一步）
const Login = () => import("@/views/login")
const Layout = () => import("@/views/layout")
const Welcome = () => import("@/views/welcome")
const NotFound = () => import("@/views/404")
const Article = () => import("@/views/article")
const Image = () => import("@/views/image")
const Publish = () => import("@/views/publish")
const Comment = () => import("@/views/comment")
const Fans = () => import(/* webpackChunkName: "fans" */"@/views/fans")
const Setting = () => import("@/views/setting")
// 初始化路由(第二步 声明组件和路径的关系)
const routes = [
    // 登录
    {
        name: "login",
        path: "/login",
        component: Login
    },
    // 首页架子
    {
        name: "",
        path: "/",
        component: Layout,
        children: [
            // 欢迎页面
            {
                path: "/",
                component: Welcome
            },
            // 文章页面
            {
                path: "/article",
                component: Article
            },
            // 素材管理
            {
                path: "/image",
                component: Image
            },
            // 发布文章
            {
                path: "/publish",
                component: Publish
            },
            // 评论管理
            {
                path: "/comment",
                component: Comment
            },
            // 粉丝管理
            {
                path: "/fans",
                component: Fans
            },
            // 个人设置
            {
                path: "/setting",
                component: Setting

            }
        ]
    },
    { path: "*", component: NotFound }
]


// 创建VueRouter实例
const router = new VueRouter({ routes })
// 导航守卫（每次跳转路由的时候执行）
router.beforeEach((to, from, next) => {
    // to 即将跳转的路由对象
    // from 正在来开的路由对象
    // next() 下一步,放行  next('地址') 拦截，跳转去哪
    const user = auth.getUser()
    // 不是访问登录，且没有token信息，不能访问其他页面，拦截到登录
    if (to.path !== '/login' && !user.token) return next('/login')
    // 其他情况一概放行
    next()
})
// 导出实例
export default router

